<template>
  <div class="home-container">
    <div class="top">
      <div class="left-charts">
        <div class="task">
          <div class="title">
            <span class="sub-title common">工单统计</span>
            <span class="time">{{ dateRangeStr }}</span>
          </div>
          <div class="content">
            <div class="total item">
              <span class="num">{{ taskTotal }}</span
              ><span class="mini-title">工单总数（个）</span>
            </div>
            <div class="done item">
              <span class="num">{{ taskDone }}</span
              ><span class="mini-title">完成工单（个）</span>
            </div>
            <div class="pending item">
              <span class="num">{{ taskPending }}</span
              ><span class="mini-title">进行工单（个）</span>
            </div>
            <div class="cancel item">
              <span class="num">{{ taskCanceled }}</span
              ><span class="mini-title">取消工单（个）</span>
            </div>
          </div>
        </div>
        <div class="sales">
          <div class="title">
            <span class="sub-title common">销售统计</span>
            <span class="time">{{ dateRangeStr }}</span>
          </div>
          <div class="content">
            <div class="order item">
              <span class="num">{{ ordercount }}</span>
              <span class="mini-title">订单量（个）</span>
            </div>
            <div class="price item">
              <span class="num">{{ income }}</span
              ><span class="mini-title">销售额（万元）</span>
            </div>
          </div>
        </div>
        <div class="sales-data">
          <div class="title">
            <div class="left">
              <span class="sub-title">销售数据</span>
              <span class="time" v-if="currentOption === 'week'">{{
                weekRangeStr
              }}</span>
              <span class="time" v-if="currentOption === 'month'">{{
                monthRangeStr
              }}</span>
              <span class="time" v-if="currentOption === 'year'">{{
                yearRangeStr
              }}</span>
            </div>
            <div class="tab">
              <div
                class="tab-item"
                :class="{ active: currentOption === 'week' }"
                @click="currentOption = 'week'"
              >
                周
              </div>
              <div
                class="tab-item"
                :class="{ active: currentOption === 'month' }"
                @click="currentOption = 'month'"
              >
                月
              </div>
              <div
                class="tab-item"
                :class="{ active: currentOption === 'year' }"
                @click="currentOption = 'year'"
              >
                年
              </div>
            </div>
          </div>
          <!-- 销售数据图表 两个 -->
          <div class="charts">
            <div
              class="left"
              ref="chart1"
              v-show="chart1Series.length !== 0"
            ></div>
            <div class="content" v-show="chart1Series.length === 0">
              <div class="null">
                <img src="../../assets/home/null.png" alt="" />
                <span>暂无数据</span>
              </div>
            </div>
            <div
              class="right"
              ref="chart2"
              v-show="chart2Series.length !== 0"
            ></div>
            <div class="content" v-show="chart2Series.length === 0">
              <div class="null">
                <img src="../../assets/home/null.png" alt="" />
                <span>暂无数据</span>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="right-charts">
        <div class="sales-rank">
          <div class="header">
            <div class="title">商品热榜</div>
            <div class="time">{{ dateRangeStr }}</div>
          </div>
          <div class="body">
            <el-row
              class="row"
              type="flex"
              v-for="(item, index) in goodsRank"
              :key="index"
            >
              <el-col :class="`rank top${index + 1}`" :span="4"
                ><span>{{ index + 1 }}</span></el-col
              >
              <el-col class="name" :span="17">{{ item.skuName }}</el-col>
              <el-col class="count" :span="6">{{ item.count }}单</el-col>
            </el-row>
          </div>
        </div>
      </div>
    </div>
    <div class="bottom">
      <div class="left">
        <div class="title">
          <span>合作商点位数Top5</span
          ><i class="el-icon-more" @click="$router.push('/node/partner')" />
        </div>
        <!-- 合作商点位数图表 -->
        <div class="node-data">
          <div class="chart" ref="chart3"></div>
          <div class="count">
            <div class="item">
              <span class="num">{{ nodeCount }}</span
              ><span class="sub-title">点位数</span>
            </div>
            <div class="item">
              <span class="num">{{ partnerCount }}</span
              ><span class="sub-title">合作商数</span>
            </div>
          </div>
        </div>
      </div>
      <div class="right">
        <div class="title">
          <span>异常设备监控</span
          ><i class="el-icon-more" @click="$router.push('/vm/index')" />
        </div>
        <div class="content">
          <div class="null">
            <img src="../../assets/home/null.png" alt="" />
            <span>暂无数据</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  dateRangeStr,
  weekRangeStr,
  monthRangeStr,
  yearRangeStr,
} from "@/utils/time";

export default {
  name: "Home",
  data() {
    return {
      dateRangeStr: "",
      weekRangeStr: "",
      monthRangeStr: "",
      yearRangeStr: "",
      currentOption: "week",
    };
  },
  computed: {
    ...mapGetters([
      "name",
      "nodeCount",
      "ordercount",
      "partnerCount",
      "partnerNode",
      "income",
      "taskCanceled",
      "taskPending",
      "taskDone",
      "taskTotal",
      "goodsRank",
      "chart1xAxis",
      "chart1Series",
      "chart2xAxis",
      "chart2Series",
    ]),
  },
  methods: {
    getChart1() {
      const chartDom1 = this.$refs.chart1;
      const myChart1 = this.$echarts.init(chartDom1);
      // 销售额趋势图
      myChart1.setOption({
        // echarts配置项
        title: {
          text: "销售额趋势图",
          left: "center",
          textStyle: {
            fontSize: 14,
          },
        },
        grid: {
          left: "10%",
          right: "10%",
          top: "20%",
          bottom: "10%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: this.chart1xAxis,
          axisLabel: {
            color: "#999",
          },
          axisLine: {
            lineStyle: {
              color: "#e0e6f1",
            },
          },
        },
        yAxis: {
          type: "value",
          name: "单位：元",
          axisLabel: {
            color: "#999",
          },
        },
        series: [
          {
            data: this.chart1Series,
            type: "line",
            smooth: true,
            areaStyle: {
              color: this.$echarts.graphic.LinearGradient(
                0,
                0,
                0,
                1,
                [
                  {
                    offset: 0,
                    color: "#ff5757",
                  },
                  {
                    offset: 1,
                    color: "#fff",
                  },
                ],
                false
              ),
              opacity: 0.3,
            },
            lineStyle: {
              width: 3,
              color: "#ff5757",
            },
            itemStyle: {
              color: "#ff5757",
              borderColor: "#ff5757",
              borderWidth: 1,
            },
          },
        ],
      });
    },
    getChart2() {
      const chartDom2 = this.$refs.chart2;
      const myChart2 = this.$echarts.init(chartDom2);
      // 销售额分布图
      myChart2.setOption({
        // echarts配置项
        title: {
          text: "销售额分布",
          left: "center",
          textStyle: {
            fontSize: 14,
          },
        },
        grid: {
          left: "10%",
          right: "10%",
          top: "20%",
          bottom: "10%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          data: this.chart2xAxis,
          axisLabel: {
            color: "#999",
          },
          axisLine: {
            lineStyle: {
              color: "#e0e6f1",
            },
          },
        },
        yAxis: {
          type: "value",
          name: "单位：元",
          axisLabel: {
            color: "#999",
            margin: 10,
          },
        },
        series: [
          {
            data: this.chart2Series,
            type: "bar",
            barWidth: 15,
            itemStyle: {
              color: "#91b0ff",
              borderRadius: [4, 4, 0, 0],
            },
          },
        ],
      });
    },
    getChart3() {
      const chartDom3 = this.$refs.chart3;
      const myChart3 = this.$echarts.init(chartDom3);
      // 合作商点位数图  done
      myChart3.setOption({
        color: ["#85a7ff", "#99ebbd", "#ffb18b", "#c6ebff", "#becce6"], // 设置调色盘颜色列表
        tooltip: {
          trigger: "item",
          formatter: function (params) {
            return `${params.name}\n${params.value}%`;
          },
        },
        series: [
          {
            name: "Access From",
            type: "pie",
            radius: ["25%", "70%"],
            roseType: "radius",
            avoidLabelOverlap: false,
            labelLine: {
              show: true,
              lineStyle: {
                color: "#bfbfbf",
              },
            },
            label: {
              show: true,
              formatter: function (params) {
                return `{a|${params.name}}\n${params.value}%`;
              },
              rich: {
                a: {
                  fontWeight: "bold",
                },
              },
            },
            data: this.partnerNode,
          },
        ],
      });
    },
  },
  async created() {
    this.$store.dispatch("home/getPartnerNode");
    this.$store.dispatch("home/getNodeCount");
    this.$store.dispatch("home/getTaskStatus");
    this.$store.dispatch("home/getOrderCount");
    this.$store.dispatch("home/getIncome");
    this.$store.dispatch("home/getPartnerCount");
    this.$store.dispatch("home/getGoodsRank");
    this.$store.dispatch("home/getOrderRegionWeek");
    this.$store.dispatch("home/getAmountWeek");
    this.dateRangeStr = dateRangeStr;
    this.weekRangeStr = weekRangeStr;
    this.monthRangeStr = monthRangeStr;
    this.yearRangeStr = yearRangeStr;
  },
  watch: {
    // 监听周月年选项选中情况
    currentOption(newVal, oldVal) {
      if (newVal === "week") {
        this.$store.dispatch("home/getOrderRegionWeek");
        this.$store.dispatch("home/getAmountWeek");
        setTimeout(() => {
          this.getChart1();
          this.getChart2();
        }, 800);
      } else if (newVal === "month") {
        this.$store.dispatch("home/getOrderRegionMonth");
        this.$store.dispatch("home/getAmountMonth");
        setTimeout(() => {
          this.getChart1();
          this.getChart2();
        }, 800);
      } else {
        this.$store.dispatch("home/getOrderRegionYear");
        this.$store.dispatch("home/getAmountYear");
        setTimeout(() => {
          this.getChart1();
          this.getChart2();
        }, 800);
      }
    },
  },
  mounted() {
    setTimeout(() => {
      this.getChart1(), this.getChart2(), this.getChart3();
    }, 1000);
  },
};
</script>

<style lang="scss" scoped>
.home-container {
  width: 100%;
  height: 100%;
  padding: 20px;
  background-color: #f8fafd;
  .top {
    display: flex;
    padding-top: 10px;
    .left-charts {
      width: 75%;
      display: flex;
      flex-wrap: wrap;
      margin-right: 20px;
      .task {
        width: 489px;
        height: 166px;
        padding: 20px;
        margin-right: 20px;
        margin-bottom: 20px;
        background-color: #e9f3ff;
        background-image: url("../../assets/home/task2.png"),
          url("../../assets/home/task1.png");
        background-repeat: no-repeat, no-repeat;
        background-position: 0 0, calc(100% - 12px) 100%;
        border-radius: 20px;
        .title {
          .sub-title {
            font-size: 16px;
            color: #333;
            font-weight: 700;
            margin-right: 10px;
          }
          .time {
            font-size: 13px;
            color: #999;
          }
        }
        .content {
          display: flex;
          justify-content: space-between;
          padding-left: 15px;
          .item {
            width: 112px;
            height: 107px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            .num {
              height: 50px;
              font-size: 36px;
              font-family: PingFangSC-Semibold, PingFang SC;
              font-weight: 600;
              line-height: 50px;
              color: #072074;
              text-shadow: 2px 4px 7px rgb(85 132 255 / 50%);
            }
            .mini-title {
              height: 17px;
              margin-top: 3px;
              font-size: 12px;
              font-family: PingFangSC-Regular, PingFang SC;
              font-weight: 400;
              color: #91a7dc;
              line-height: 17px;
            }
          }
        }
      }
      .sales {
        width: 400px;
        height: 166px;
        padding: 20px;
        background: #fbefe8 url("../../assets/home/salespig.png") no-repeat
          calc(100% - 12px) 100%;
        border-radius: 20px;
        .title {
          .sub-title {
            font-size: 16px;
            color: #333;
            font-weight: 700;
            margin-right: 10px;
          }
          .time {
            font-size: 13px;
            color: #999;
          }
        }
        .content {
          display: flex;
          justify-content: space-around;
          padding-left: 15px;
          .item {
            width: 112px;
            height: 107px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            .num {
              height: 50px;
              font-size: 36px;
              font-family: PingFangSC-Semibold, PingFang SC;
              font-weight: 600;
              line-height: 50px;
              color: #ff5757;
              text-shadow: 2px 4px 7px rgb(255 99 85 / 50%);
            }
            .mini-title {
              height: 17px;
              margin-top: 3px;
              font-size: 12px;
              font-family: PingFangSC-Regular, PingFang SC;
              font-weight: 400;
              color: #de9690;
              line-height: 17px;
            }
          }
        }
      }
      .sales-data {
        width: 919px;
        height: 352px;
        background-color: #fff;
        border-radius: 20px;
        padding: 20px;
        .title {
          display: flex;
          justify-content: space-between;
          .sub-title {
            font-size: 16px;
            color: #333;
            font-weight: 700;
            margin-right: 10px;
          }
          .time {
            font-size: 13px;
            color: #999;
          }
          .tab {
            cursor: pointer;
            width: 120px;
            height: 35px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            background-color: #f7fbff;
            border-radius: 10px;
            padding: 5px 10px;
            .tab-item {
              width: 37px;
              height: 25px;
              font-size: 14px;
              color: #9ca3b4;
              text-align: center;
              line-height: 25px;
              padding: 0 10px;
            }
            .active {
              color: #333;
              background-color: #fff;
              font-weight: 600;
              border-radius: 7px;
              box-shadow: 0 0 4px 0 rgb(0 0 0 / 11%);
            }
          }
        }
        .charts {
          display: flex;
          margin-top: 25px;
          .left {
            width: 440px;
            height: 278px;
            margin-right: 20px;
          }
          .right {
            width: 440px;
            height: 278px;
          }
          .content {
            width: 440px;
            height: 278px;
            padding-top: 50px;
            .null {
              display: flex;
              flex-direction: column;
              align-items: center;
              img {
                margin-bottom: 25px;
              }
              span {
                font-size: 14px;
                color: #20232a;
              }
            }
          }
        }
      }
    }
    .right-charts {
      width: 25%;
      padding: 20px;
      background-color: #fff;
      border-radius: 20px;
      .sales-rank {
        .header {
          display: flex;
          .title {
            font-size: 16px;
            color: #333;
            font-weight: 700;
            margin-right: 10px;
          }
          .time {
            padding-top: 4px;
            font-size: 13px;
            color: #999;
          }
        }
        .body {
          .row {
            margin: 25px 0;
          }
          .top1 {
            color: #8e5900;
            background: url("../../assets/home/no.1.png") no-repeat;
          }
          .top2 {
            color: #494949;
            background: url("../../assets/home/no.2.png") no-repeat;
          }
          .top3 {
            color: #cf6d3d;
            background: url("../../assets/home/no.3.png") no-repeat;
          }
          .top4,
          .top5,
          .top6,
          .top7,
          .top8,
          .top9,
          .top10 {
            color: #e9b499;
            background: url("../../assets/home/label.png") no-repeat;
            background-position: 2px 0;
          }
          .rank {
            height: 20px;
            padding-right: 17px;
            text-align: center;
            padding-top: 0;
            font-size: 12px;
          }
          .name {
            color: #333;
            font-size: 14px;
          }
          .count {
            color: #737589;
            font-size: 15px;
            text-align: right;
          }
        }
      }
    }
  }
  .bottom {
    display: flex;
    margin-top: 20px;
    .left {
      width: 710px;
      height: 353px;
      padding: 20px;
      background-color: #fff;
      border-radius: 20px;
      margin-right: 20px;
      .title {
        display: flex;
        justify-content: space-between;
        color: #333;
        font-size: 16px;
        font-weight: 700;
        .el-icon-more {
          color: #5f84ff;
          cursor: pointer;
        }
      }
      .node-data {
        display: flex;
        align-items: center;
        .chart {
          width: 469px;
          height: 284px;
          margin-top: 20px;
        }
        .count {
          width: 154px;
          height: 230px;
          padding-top: 47px;
          padding-left: 38px;
          background: linear-gradient(135deg, transparent, #f8f8f9 0) 0 0,
            linear-gradient(-135deg, transparent 12px, #f8f8f9 0) 100% 0,
            linear-gradient(-45deg, transparent, #f8f8f9 0) 100% 100%,
            linear-gradient(45deg, transparent 12px, #f8f8f9 0) 0 100%;
          background-size: 50% 50%;
          background-repeat: no-repeat;

          .item {
            display: flex;
            flex-direction: column;
            margin-bottom: 20px;
            .num {
              height: 33px;
              font-size: 24px;
              font-family: PingFangSC-Semibold, PingFang SC;
              font-weight: 600;
              color: #072074;
              line-height: 33px;
              margin-bottom: 10px;
            }
            .sub-title {
              font-size: 12px;
              color: #000412;
            }
          }
        }
      }
    }
    .right {
      position: relative;
      width: 502px;
      height: 353px;
      background-color: #fff;
      border-radius: 20px;
      padding: 20px;
      .title {
        display: flex;
        justify-content: space-between;
        color: #333;
        font-size: 16px;
        font-weight: 700;
        .el-icon-more {
          color: #5f84ff;
          cursor: pointer;
        }
      }
      .content {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        .null {
          display: flex;
          flex-direction: column;
          align-items: center;
          img {
            margin-bottom: 25px;
          }
          span {
            font-size: 14px;
            color: #20232a;
          }
        }
      }
    }
  }
}
</style>
